package i_thread;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ThreadWaitTest2 extends JFrame {

	WaitTest wt, wt2 ;
	
	private JPanel contentPane;
	private JPanel panel;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private JTextArea textArea;
	private JButton btnNewButton_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ThreadWaitTest2 frame = new ThreadWaitTest2();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ThreadWaitTest2() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.add(getBtnNewButton_2());
			panel.add(getBtnNewButton());
			panel.add(getBtnNewButton_1());
		}
		return panel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("Wait");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(wt != null){
						synchronized (wt) {
							try{
								wt.wait();
							}catch(Exception ex){
								
							}
						}
					}
				}
			});
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("Notify");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(wt != null){
						wt.n();
					}
				}
			});
		}
		return btnNewButton_1;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("Start");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(wt == null){
						wt = new WaitTest(textArea);
						wt2 = new WaitTest(textArea);
						
						wt.start();
						wt2.start();
						
						synchronized (wt) {
							try{
							wt.wait();
							}catch(Exception ex){
								
							}
						}
					}
				}
			});
		}
		return btnNewButton_2;
	}
}

class WaitTest extends Thread{
	int a=0;
	JTextArea ta;
	boolean b=false;
	public WaitTest(JTextArea ta){
		this.ta = ta;
	}
	@Override
	public void run(){
		synchronized (this) {
			try{
				//wait();
				process();
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		
	}
	void process(){

		while(a<1000){
			a++;
			ta.append(" "+a);
			System.out.print(a + " ");
			
			
			if(a%10==0){
				ta.append("\n");
				System.out.println();
			}
			
			try{
				this.sleep(500);
			}catch(Exception ex){
				ex.printStackTrace();
				
			}

		}
		
	}
	
	public void check(boolean b) throws Exception {
		if(b){
			this.wait();
		}else{
			this.notifyAll();
		}
	}
	 public void w(){
		try{
			check(true);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void n(){
		try{
			check(false);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
}
